// pages/posts/post-detail/post-detail.js
var postData = require('../../../data/posts-data.js')
var WxParse = require('../../../wxParse/wxParse.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    baseURL: 'https://shipin.lekuaiwang.com',
    postId: '',
    isPlaying: false,
    inpHidden: false,
    isExpose: false,
    isCheck: true,
    _num: 1,
    article: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var _this = this
    this.setData({
      zan: false
    })
    // 文章编号的本地数据化
    var postId = options.id
    console.log(postId)
    this.setData({
      postId: postId
    })
    var openid = wx.getStorageSync('openid')

    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/resource/detail?id=' + postId + '&openid=' + openid,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      success: function (res) {
        // success
        console.log(openid)
        console.log(res.data.data)
        _this.setData({
          post_detail: res.data.data
        })
        var article = _this.data.post_detail.content
        WxParse.wxParse('article', 'html', article, _this, 5)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
    // 获取评论
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/top?id=' + postId + '&openid=' + openid,
      data: {},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        console.log(openid)
        // success
        console.log(res.data)
        _this.setData({
          all_comments: res.data.data
        })

      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

    // 获取文章数据，设置到本地 data 中
    // var postDetail = postData.postList[postId]
    // this.setData({
    //   postDetail: postDetail
    // })
    // 收藏状态与收藏标志的设定
    // var postsCollected = wx.getStorageSync('posts_Collected')
    // var postCollected = postsCollected[postId]
    // if (!postsCollected) {
    //   var postsCollected = {}
    // }
    // if (postCollected) {
    //   this.setData({
    //     collected: postCollected
    //   })
    // } else {
    //   postsCollected[postId] = false
    //   wx.setStorageSync('posts_Collected', postsCollected)
    // }

  },
  onComZanTap: function (e) {
    var comIndex = e.currentTarget.dataset.index
    var id = e.currentTarget.dataset.id
    var openid = wx.getStorageSync('openid')
    var all_comments = this.data.all_comments
    // console.log(this.data.all_comments.data[comIndex].is_good)
    var _this = this
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/good?id=' + id + '&openid=' + openid,
      data: {},
      method: 'GET',
      success: function (res) {
        // success
        console.log(res)
        all_comments[comIndex].is_good = !(all_comments[comIndex].is_good)
        console.log(all_comments)
        _this.setData({
          all_comments: all_comments
        })
        if (res.data.code == 200) {
          console.log('coll')
          _this.setData({
            all_comments: all_comments
          })
          if (all_comments[comIndex].is_good) {
            all_comments[comIndex].good_count = all_comments[comIndex].good_count + 1
            _this.setData({
              all_comments: all_comments
            })
          } else {
            all_comments[comIndex].good_count = all_comments[comIndex].good_count - 1
            _this.setData({
              all_comments: all_comments
            })
          }
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  onCollectTap: function (event) {
    // var zan = this.data.zan
    // this.setData({
    //   zan: !zan
    // })
    var postId = this.data.postId
    var openid = wx.getStorageSync('openid')
    var post_detail = this.data.post_detail
    console.log('bad')
    var _this = this
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/resource/collection?id=' + postId + '&openid=' + openid,
      data: {},
      method: 'GET',
      success: function (res) {
        // success
        console.log(res)
        // post_detail.is_good = !(post_detail.is_good)
        // console.log(post_detail[postId])
        if (res.data.code == 200) {
          console.log('coll')
          _this.setData({
            'post_detail.is_collection': !post_detail.is_collection
          })
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  delComment: function (e) {
    console.log(123)
    var _this = this
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var openid = wx.getStorageSync('openid')
    console.log(openid)
    var all_comments = this.data.all_comments
    wx.showModal({
      title: '删除评论',
      content: '请确认是否删除评论',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: 'https://shipin.lekuaiwang.com/api/talk/delete?id=' + id + '&openid=' + openid,
            method: "POST",
            data: {
            },
            header: {
              "content-type": "application/x-www-form-urlencoded;charset=utf-8",
            },
            success: res => {
              all_comments.splice(index, 1)
              _this.setData({
                all_comments: all_comments
              })
              wx.showToast({
                title: "删除成功"
              })
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  getTalk: function (e) {
    var val = e.detail.value
    console.log(val)
    this.setData({
      val: val
    })
  },
  submitComment: function (e) {
    var content = this.data.val
    console.log(content)
    var openid = wx.getStorageSync('openid')
    var id = this.data.post_detail.id
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/add?res_id=' + id + '&openid=' + openid,
      data: {
        content: content
      },
      method: 'POST',
      success: function (res) {
        // success
        console.log(res)
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  onReplyTap: function (e) {
    console.log(900)
    console.log(e)
    var commentid = e.currentTarget.dataset.commentid
    wx.navigateTo({
      url: '/pages/comment-detail/comment-detail?id=' + commentid
    })
  },
  toggleCheck: function (e) {
    console.log(e.currentTarget.dataset.content)
    this.setData({
      _num: e.currentTarget.dataset.num,
      _reason: e.currentTarget.dataset.content
    })
  },
  getReason: function (e) {
    var val = e.detail.value
    console.log(val)
    this.setData({
      reason_val: val
    })
  },
  exposeTap: function (e) {
    console.log(e.currentTarget.dataset)
    var dataset = e.currentTarget.dataset
    this.setData({
      isExpose: true,
      expose_talk: {
        target_user_id: dataset.talk_user_id,
        talk_id: dataset.id,
      }
    })
  },
  exposeFun: function (e) {
    var _this = this
    var openid = wx.getStorageSync('openid')
    this.setData({
      isExpose: false,
    })
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/talk/report',
      data: {
        openid: openid,
        target_user_id: _this.data.expose_talk.talk_user_id,
        talk_id: _this.data.expose_talk.talk_user_id,
        reason: _this.data.content,
        content: _this.data.reason_val
      },
      method: 'POST',
      // header: {}, // 设置请求的 header
      success: function (res) {
        // success
        console.log(res)
        if (res.data.code == 200) {
          wx.showToast({
            title: "举报成功"
          })
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })

  },

  // showInp: function (e) {
  //   this.setData({
  //     inpHidden: true
  //   })
  // },
  onZanTap: function (event) {
    var postId = this.data.postId
    var openid = wx.getStorageSync('openid')
    var post_detail = this.data.post_detail
    console.log('hello')
    var _this = this
    wx.request({
      url: 'https://shipin.lekuaiwang.com/api/resource/good?id=' + postId + '&openid=' + openid,
      data: {},
      method: 'GET',
      success: function (res) {
        // success
        console.log(res)
        // post_detail.is_good = !(post_detail.is_good)
        // console.log(post_detail[postId])
        if (res.data.code == 200) {
          console.log('good')
          _this.setData({
            'post_detail.is_good': !post_detail.is_good
          })

          if (post_detail.is_good) {
            _this.setData({
              'post_detail.good_count': post_detail.good_count + 1
            })
          } else {
            _this.setData({
              'post_detail.good_count': post_detail.good_count - 1
            })
          }
        }
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
    // var postsCollected = this.data.post_detail.is_good
    // var currentCollect = postsCollected[this.data.postId]

    // // if (currentCollect != true){
    // //   currentCollect = true
    // //   postsCollected[this.data.postId] = currentCollect
    // // }else {
    // //   currentCollect = false
    // //   postsCollected[this.data.postId] = currentCollect
    // // }
    // // 收藏与否的设定
    // currentCollect = !currentCollect
    // // 更新本地化缓存中收藏的状态
    // postsCollected[this.data.postId] = currentCollect
    // this.showModal(postsCollected, currentCollect)
  },
  showModal: function (postsCollected, currentCollect) {
    var that = this
    wx.showModal({
      title: '收藏文章',
      content: currentCollect ? '收藏该文章？' : '取消收藏该文章？',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#333',
      confirmText: '确认',
      confirmColor: '#405f80',
      success: function (res) {
        if (res.confirm) {
          // 更新缓存中收藏的状态
          wx.setStorageSync('posts_Collected', postsCollected)
          // 更新当前文章的收藏状态
          that.setData({
            collected: currentCollect
          })
        }
      }
    })
  },
  onShare: function () {
    console.log(123)
    wx.showShareMenu()
  },
  onShareTap: function (event) {
    wx.showActionSheet({
      itemList: ['分享给微信好友', '分享到朋友圈', '分享到QQ', '分享到微博'],
      itemColor: '#405f80',
      success: function (res) {
        console.log(res.tapIndex)
      }
    })
  },
  onMusicTap(event) {
    // console.log(event)
    // wx.getBackgroundAudioManager({
    //   dataUrl: 'http://ws.stream.qqmusic.qq.com/C100003507bR0gDKBm.m4a?fromtag=38',
    //   title: '夜夜夜夜-齐秦',
    //   coverImgUrl: 'http://y.gtimg.cn/music/photo_new/T002R150x150M000001TEc6V0kjpVC.jpg?max_age=2592000',
    //   success: function(res) {},
    //   fail: function(res) {},
    //   complete: function(res) {},
    // })
    var isPlaying = this.data.isPlaying
    const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.title = '此时此刻'
    backgroundAudioManager.epname = '此时此刻'
    backgroundAudioManager.singer = '许巍'
    backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
    // 设置了 src 之后会自动播放
    backgroundAudioManager.src = 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46'
    if (isPlaying) {
      // 暂停
      BackgroundAudioManager.pause()
      this.data.isPlaying = true
    } else {
      // 播放
      this.data.isPlaying = false
    }
  }

})